package com.qianya.weishi_server.config.security;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.util.ClassUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * @author LLL
 */
public class TokenAuthenticationProvider implements AuthenticationProvider {

    private SecurityProviderManager providerManager;

    TokenAuthenticationProvider(SecurityProviderManager providerManager) {
        this.providerManager = providerManager;
    }


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        StatelessTokenAuthentication tokenAuth = (StatelessTokenAuthentication) authentication;
        StatelessTokenAuthentication.Credentials credentials = (StatelessTokenAuthentication.Credentials) tokenAuth.getCredentials();

        HttpServletRequest request = credentials.getRequest();
        return providerManager.parseToken(request);
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return ClassUtils.isAssignable(StatelessTokenAuthentication.class, authentication);
    }
}
